πŸ‘©β€πŸ’» 17680. μΊμ‹œ

@Seongukbaek Posted May 16, 2022 Β· Updated May 16, 2022 Β· 2 min read

문제

Programmers - μΊμ‹œ

πŸ“ Logic

for (String city : cities) { city = city.toLowerCase(); // 이미 μΊμ‹œμ— μžˆλ‹€λ©΄ if (cache.contains(city)) { cache.remove(city); time++; } else { // μΊμ‹œμ— μ—†λŠ”λ°, 더 이상 μΊμ‹œ 곡간도 μ—†λŠ” 경우, 처음 λ“€μ–΄μ˜¨ 값을 μ‚­μ œ if (cache.size() >= cacheSize) cache.poll(); time += 5; } cache.add(city); }
  • Queue λ₯Ό μ‚¬μš©ν•΄ λ“€μ–΄μ˜¨ λ„μ‹œμˆœμœΌλ‘œ μΊμ‹±ν•œλ‹€.
  • Hit 이 λ°œμƒν•œλ‹€λ©΄, ν•΄λ‹Ή λ„μ‹œλ₯Ό μ§€μš°κ³  λ‹€μ‹œ 맨 뒀에 μΆ”κ°€ν•œλ‹€.

πŸ“„ CODE

μ½”λ“œ 보기/μ ‘κΈ°πŸ’«
import java.util.*; class Solution { // λ„μ‹œ 이름이 λ“€μ–΄μ˜¨ μˆœμ„œλ₯Ό μ €μž₯ν•˜κΈ° μœ„ν•œ 큐 Queue<String> cache = new LinkedList<>(); public int solution(int cacheSize, String[] cities) { int time = 0; // cacheSizeκ°€ 0이면 μΊμ‹œ νž›μ΄ λ°œμƒν•˜μ§€ μ•ŠμŒ if (cacheSize == 0) return cities.length * 5; for (String city : cities) { city = city.toLowerCase(); // 이미 μΊμ‹œμ— μžˆλ‹€λ©΄ if (cache.contains(city)) { cache.remove(city); time++; } else { // μΊμ‹œμ— μ—†λŠ”λ°, 더 이상 μΊμ‹œ 곡간도 μ—†λŠ” 경우, 처음 λ“€μ–΄μ˜¨ 값을 μ‚­μ œ if (cache.size() >= cacheSize) cache.poll(); time += 5; } cache.add(city); } return time; } }

✏️ Review

  • 맀우 μ‰¬μš΄ λ¬Έμ œμ˜€μ§€λ§Œ, LRU μ•Œκ³ λ¦¬μ¦˜μ„ λŒ€μΆ© μƒκ°ν•˜κ³  ν’€μ—ˆλ‹€κ°€ μ‹œκ°„ 날렀버렸닀. 이런...
@Seongukbaek
배우고 μ •λ¦¬ν•˜κ³  κ³΅μœ ν•˜λŠ” λŒ€ν•™μƒ λ°±μ„±μš±μž…λ‹ˆλ‹€ πŸ˜€